<?php


namespace app\manage\controller;


use app\manage\model\Admin as AdminModel;
use app\manage\validate\Admin as AdminValidate;
use think\Request;

class Admin extends Base
{
    public function index()
    {
        $admin = AdminModel::where('status', '=', 1)->order('id', 'asc')->paginate(1);
        $this->assign('admin', $admin);
        return $this->fetch('admin/index');
    }

    public function create()
    {
        if ($this->request->isPost()) {
            $validator = (new AdminValidate());
            $res = $validator->goCheck();
            if (!$res) {
                $this->redirect('admin/create');
            }
            $params = Request::instance()->param();
            $admin = new AdminModel();
            if ($admin->allowField(true)->isUpdate(false)->save($params)) {
                $this->redirect('admin/index', [], 200);
            }
            $this->redirect('admin/create');
        }
        return $this->fetch('admin/create');
    }

    public function detail($id)
    {

    }

    public function edit($id)
    {
        $admin = AdminModel::get($id);
        if ($this->request->isGet()) {

        }

    }

    public function delete($id = null)
    {
        $id = $this->request->param('id');
        $admin = AdminModel::get($id);
        if (empty($admin)) {
            $this->error(`没有找到id号为{$id}的记录`);
            return;
        }
        $admin->delete();
        $this->redirect('admin/index', [], 200);
    }
}